package com.mobiporter.adias.tests;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

import com.mobiporter.utils.*;

public class StringUtilTest 
{
	@Test
	public void testGetGroupWithNull()
	{
		String pattern = "aouioue";
		String input = "aouou" ;
		int groupNo = -1 ;
		Assert.assertNull(StringUtils.getGroup(groupNo, pattern, input));
		
		pattern = null ;
		groupNo = 1 ;
		Assert.assertNull(StringUtils.getGroup(groupNo, pattern, input));
		
		pattern = "aououou";
		input = null ;
		Assert.assertNull(StringUtils.getGroup(groupNo, pattern, input));
		
	}
	
	@Test
	public void testGetGroupWithProperInput()
	{
		String pattern = "(\\d)-(\\d)-(\\d)";
		String input = "1-4-7";
		
		int groupNo = 1 ;
		Assert.assertEquals(1+"", StringUtils.getGroup(groupNo, pattern, input));
		
		groupNo = 2 ;
		Assert.assertEquals(4+"", StringUtils.getGroup(groupNo, pattern, input));
		
		groupNo = 3 ;
		Assert.assertEquals(7+"", StringUtils.getGroup(groupNo, pattern, input));

	}
	
	@Test
	public void testGroupWithWrongInput() 
	{
		String pattern = "(\\d)-(\\d)-(\\d)";
		String input = "1/4-7";
		
		int groupNo = 1 ;
		Assert.assertNull(StringUtils.getGroup(groupNo, pattern, input));
	
	}
	
	@Test
	public void testInputStreamToString()
	{
		String testString = "ABCDEEEEEEE!!@!@#!#!@#!#!#!#!#!#!#!#!#!#!#!#!#!#!";
		InputStream is =  new ByteArrayInputStream(testString.getBytes());
		Assert.assertEquals(testString+"\n", StringUtils.getStringFromInputStream(is));
	}
	
	
	@Test
	public void testFormatMap()
	{
		Map<String,String> airlineToTaxMap = new HashMap<String, String>();
		Assert.assertEquals("", StringUtils.formatMap(airlineToTaxMap));
		
		airlineToTaxMap.put("Kingfisher", "22334.0");
		Assert.assertEquals("Kingfisher:22334.0", StringUtils.formatMap(airlineToTaxMap));
		
		airlineToTaxMap.put("Indian Airlines", "22113.0");
		Assert.assertEquals("Indian Airlines:22113.0|Kingfisher:22334.0", StringUtils.formatMap(airlineToTaxMap));
	}
	
	@Test
	public void testParseMap()
	{
		String airlineTaxString = "Indian Airlines:22113.0";
		Map<String,String> airlineTaxMap = StringUtils.parseMap(airlineTaxString);
		
		Assert.assertTrue(airlineTaxMap.containsKey("Indian Airlines"));
		Assert.assertEquals(airlineTaxMap.get("Indian Airlines"), "22113.0");
		
		airlineTaxString = "Indian Airlines:22113.0|Kingfisher:22334.0";
		airlineTaxMap = StringUtils.parseMap(airlineTaxString);
		
		Assert.assertTrue(airlineTaxMap.containsKey("Indian Airlines"));
		Assert.assertEquals(airlineTaxMap.get("Indian Airlines"), "22113.0");
		Assert.assertTrue(airlineTaxMap.containsKey("Kingfisher"));
		Assert.assertEquals(airlineTaxMap.get("Kingfisher"), "22334.0");
		
	}
	
	@Test
	public void testGetItemOnIndex()
	{
		String string = "192.168.1.1,www.ixigo.com";
		Assert.assertEquals("192.168.1.1", StringUtils.getItemOnIndex(string,",", 0));
		Assert.assertEquals("www.ixigo.com", StringUtils.getItemOnIndex(string,",", 1));
	}
	
}
